package com.limm.servlet.login;

import com.limm.dao.login.LoginDao;
import com.limm.util.DbUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    LoginDao loginDao = new LoginDao();
    Map map = new HashMap();
    String msg = "message";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        if (loginDao.isExist(email,password)){
            //设置session
            HttpSession session = request.getSession();
            session.setAttribute("myself",loginDao.getId(email));
            session.setMaxInactiveInterval(60*60);
            map.put(msg,2);
        }else{
            map.put(msg,1);
        }
        JSONObject json = JSONObject.fromObject(map);
        response.getWriter().print(json.toString());
        DbUtil.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
